package com.example.mycontentprovider;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MyProvider extends ContentProvider {
    //定义一个rui路径匹配器，如果路径匹配不成功则返回-1；
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int SUCCESS = 1;      //uri路径是/info，匹配成功时的返回码
    private MyDBHelper myDBHelper;        //数据库帮助类对象

    //使用静态代码块添加路径匹配规则
    static {
        uriMatcher.addURI("com.example.mycontentprovider", "info", SUCCESS);
    }
    public MyProvider() {
    }
    @Override
    public boolean onCreate() {  //当内容提供者对象被创建时调用
        myDBHelper = new MyDBHelper(getContext());         //getContext() 获取上下文对象context
        return false;
    }
    @Override
    public String getType(Uri uri) {
        return null;
    }
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code = uriMatcher.match(uri);
        if (code == SUCCESS) {
            SQLiteDatabase db = myDBHelper.getWritableDatabase();
            long rowId = db.insert("info", null, values);
            if (rowId > 0) {
                //使用ContentUris类的静态方法withAppendedId()方法在uri上添加rowId
                Uri insertedUri = ContentUris.withAppendedId(uri, rowId);
                //提示数据库的内容变化了
                getContext().getContentResolver().notifyChange(uri, null);
                return insertedUri;
            }
            db.close();
            return uri;
        } else {
            throw new UnsupportedOperationException("路径不正确，无法插入数据！");
        }
    }
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int code = uriMatcher.match(uri);
        if (code == SUCCESS) {
            SQLiteDatabase db = myDBHelper.getWritableDatabase();
            int count = db.delete("info", selection, selectionArgs);
            if (count > 0) { //提示数据库内容发生了变化
                getContext().getContentResolver().notifyChange(uri, null);
            }
            db.close();
            return count;  //返回删除的记录行数
        }else{
            throw new UnsupportedOperationException("路径不正确，无法删除数据！");
        }
    }
    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int code=uriMatcher.match(uri);
        if(code==SUCCESS){
            SQLiteDatabase db=myDBHelper.getWritableDatabase();
            int count=db.update("info",values,selection,selectionArgs);
            if(count>0){
                getContext().getContentResolver().notifyChange(uri,null);
            }
            db.close();
            return count;
        }else {
            throw new UnsupportedOperationException("路径不正确，无法更新数据！");
        }
    }
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int code=uriMatcher.match(uri);
        if(code==SUCCESS){
            SQLiteDatabase db=myDBHelper.getReadableDatabase();
            return db.query("info",projection,selection,selectionArgs,null,null,sortOrder);
        }else {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }
}
